package day21.pack8;

import java.util.concurrent.*;

public class Test5 {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                5,
                3 * 1000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        threadPoolExecutor.allowCoreThreadTimeOut(true);

        for (int i = 0; i < 16; i++) {
            int finalI = i;
            threadPoolExecutor.execute(()->{
                System.out.println(finalI +"任务模拟开始" + Thread.currentThread().getId());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(finalI +"任务模拟结束" + Thread.currentThread().getId());
            });
        }

        Thread.sleep(7000);
        System.out.println("第二次任务");
        for (int i = 0; i < 10; i++) {
            threadPoolExecutor.execute(()->{
                System.out.println("任务模拟开始" + Thread.currentThread().getId());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("任务模拟结束" + Thread.currentThread().getId());
            });
        }

    }
}
